# -*- coding:UTF-8 -*-
"""
@Project : SeleniumAutoTest
@File    : example3-dakaichuangkou.py
@IDE     : PyCharm
@Author  : WorldWind(1772830365@qq.com)
@Address : China - HeiLongjiang - Herbin
@Date    : 2025/8/3 下午2:27
"""
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service

service = Service("D:\chromedriver-win64\chromedriver.exe")  # 配置chromedriver文件
dr = webdriver.Chrome(service=service)
dr.maximize_window()
dr.get("http://www.baidu.com")
dr.find_element(By.ID, 'kw').send_keys('黑神话悟空')
# dr.find_element(By.ID, 'su').click()
print(dr.window_handles)
print(dr.current_window_handle)
print(dr.current_url)
sleep(3)
dr.execute_script('window.open("https://www.bing.com")')
print(dr.window_handles)
print(dr.current_window_handle)
print(dr.current_url)
# dr.switch_to.window(dr.window_handles[1])  # 切换窗口,从www.baidu.com --> 切换到 www.bing.com
handleList = dr.window_handles
for handle in handleList:
    dr.switch_to.window(handle)  # 进行窗口切换操作
    if 'bing.com' in dr.current_url:  # 如果切换到了bing的窗口
        dr.find_element(By.ID, 'sb_form_q').send_keys('大话西游')
    else:
        pass
sleep(3)
# dr.find_element(By.XPATH, '/html/body/div[1]/div/div[3]/div[2]/form/div[1]/input').send_keys("大话西游")
# sleep(3)
# # dr.find_element(By.XPATH, '//*[@id="search_icon"]/svg').click()
# sleep(3)
